package com.moon.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.moon.dao.StudentMapper;
import com.moon.entity.Student;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Transactional
@Service
public class StudentService {
	@Autowired
	private StudentMapper studentMapper;

	public StudentMapper getStudentMapper() {
		return studentMapper;
	}
	@CachePut(cacheNames="cache0")//redis注解
	public List<Student> selectStudent(String depart,String sex,int pageNum,int pageSize){
		Example exm = new Example(Student.class);
		Criteria crt = exm.createCriteria();
		if(!"".equals(depart)) {
			crt.andCondition("department=",depart);
		}
		if(!"".equals(sex)) {
			crt.andCondition("sex=",sex);
		}
		int count = studentMapper.selectCountByExample(exm);
		PageHelper.startPage(pageNum, pageSize);
		List<Student> list = studentMapper.selectByExample(exm);
		//MyPage<Student> page = new MyPage<Student>(count,pageSize,list);
		return list;
	}
	@CacheEvict(cacheNames="cache0",allEntries = true)
	public void deletedata(Integer id) {
		Example exm = new Example(Student.class);
		Criteria crt = exm.createCriteria();
		if(id!=null) {
			crt.andCondition("id=",id);
		}
		studentMapper.deleteByExample(exm);
	}
	public void updatedata() {
		System.out.println("修改成功");
	}
}